package com.settlement.system.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.EasyExcelFactory;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.settlement.system.common.annotation.PreventDuplicateSubmit;
import com.settlement.system.common.result.PageResult;
import com.settlement.system.common.result.Result;
import com.settlement.system.listener.easyexcel.SysFlowMarketListener;
import com.settlement.system.model.entity.SysFlowMarket;
import com.settlement.system.model.form.SysFlowMarketForm;
import com.settlement.system.model.query.SysFlowMarketQuery;
import com.settlement.system.model.vo.FlowPublishVO;
import com.settlement.system.model.vo.SysFlowMarketImportVo;
import com.settlement.system.service.SysFlowMarketService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * <p>
 * 流程市场表 前端控制器
 * </p>
 *
 * @author huangrensen
 * @since 2023-12-28
 */
@RestController
@RequestMapping("/api/v1/flowMarket")
@RequiredArgsConstructor
public class SysFlowMarketController {

    private final SysFlowMarketService sysFlowMarketService;

    @GetMapping("/page")
    public PageResult<SysFlowMarket> getPage(@ParameterObject SysFlowMarketQuery queryParams) {
        Page<SysFlowMarket> result = sysFlowMarketService.getPage(queryParams);
        return PageResult.success(result);
    }


    @Operation(summary = "获取流程市场表信息", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/{id}/form")
    public Result<SysFlowMarketForm> getSysFlowMarketFormData(@Parameter(description = "主键ID") @PathVariable Long id) {
        SysFlowMarketForm sysFlowMarket = sysFlowMarketService.getSysFlowMarketFormData(id);
        return Result.success(sysFlowMarket);
    }

    @Operation(summary = "发布脚本到正式分类", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/publish")
    @PreventDuplicateSubmit
    public Result<SysFlowMarketForm> publish(@RequestBody FlowPublishVO flowPublishVO) {
        sysFlowMarketService.publish(flowPublishVO.getId(), flowPublishVO.getType(), flowPublishVO.getContent());
        return Result.success();
    }

    @Operation(summary = "删除流程市场表", security = {@SecurityRequirement(name = "Authorization")})
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:flowMarket:delete')")
    public Result<?> deleteSysFlowMarket(@Parameter(description = "ID，多个以英文逗号(,)分割") @PathVariable String ids) {
        boolean result = sysFlowMarketService.deleteSysFlowMarket(ids);
        return Result.judge(result);
    }


    @Operation(summary = "流程市场表导入", security = {@SecurityRequirement(name = "Authorization")})
    @PostMapping("/sysFlowMarketImport")
    @PreAuthorize("@ss.hasPerm('sys:flowMarket:import')")
    @PreventDuplicateSubmit
    public Result<?> sysFlowMarketImport(@RequestParam("file") MultipartFile file) throws Exception {
        SysFlowMarketListener listener = new SysFlowMarketListener();
        EasyExcelFactory.read(file.getInputStream(), SysFlowMarketImportVo.class, listener).sheet(0).doRead();
        List<SysFlowMarketImportVo> list = listener.getVos();
        if (list == null || list.size() == 0) {
            return Result.success();
        }
        sysFlowMarketService.importData(list);
        return Result.success();
    }

    @Operation(summary = "导出流程市场表", security = {@SecurityRequirement(name = "Authorization")})
    @GetMapping("/_export")
    @PreAuthorize("@ss.hasPerm('sys:flowMarket:export')")
    @PreventDuplicateSubmit
    public void export(@RequestParam("ids") String ids, HttpServletResponse response) throws IOException {
        String fileName = "流程市场表.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, StandardCharsets.UTF_8));

        List<SysFlowMarketImportVo> sysFlowMarketList = sysFlowMarketService.findByIds(ids);
        EasyExcel.write(response.getOutputStream(), SysFlowMarketImportVo.class).sheet("流程市场表").doWrite(sysFlowMarketList);
    }
}

